package com.adingxiong.cft.aop;

import com.adingxiong.cft.annotate.CacheException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName CacheAspect
 * @Description   redis 缓存切面  避免redis宕机出现异常问题
 * @Author xiongchao
 * @Date 2020/10/14 13:56
 **/
@Component
@Aspect
@Order(2)
@Slf4j
public class CacheAspect {

    @Pointcut("execution( * com.adingxiong.cft.service.*CacheService.*(..))")
    public void pointCut(){

    }

    @Around("pointCut()")
    public Object doAroud(ProceedingJoinPoint pj) throws Throwable {
        Signature signature = pj.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Object result = null;

        try {
            result = pj.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            if(method.isAnnotationPresent(CacheException.class)){
                throw throwable;
            }else{
                log.error(throwable.getMessage());
            }
        }
        return result;
    }
}
